<?php
/**
*
* @package ppkBB3cker
* @version $Id: ucp_add1_leech.php 1.000 2009-05-11 15:02:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$sort_opt[$torrent_opt]=array('topic_title', 'downloaded', 'uploaded', 'ratio', 'startdat', 'to_go', 'connectable', 'upspeed', 'downspeed', 'ip', 'port', 'agent');
if(!in_array($opt_param, $sort_opt[$torrent_opt]))
{
	$opt_param=$sort_opt[$torrent_opt][0];
}

if($opt_param=='ratio')
{
	$sql_addon="p.uploaded/p.downloaded $opt_sort";
}
else if($opt_param=='upspeed')
{
	$sql_addon="p.uploadoffset/(p.last_action-p.prev_action) $opt_sort";
}
else if($opt_param=='downspeed')
{
	$sql_addon="p.downloadoffset/(p.last_action-p.prev_action) $opt_sort";
}
else if($opt_param=='agent')
{
	$sql_addon="p.agent $opt_sort";
}
else
{
	$sql_addon="$opt_param $opt_sort";
}

$sql='SELECT p.*, ip, t.topic_title, t.topic_first_post_id, tt.size, p.prev_action, p.agent, p.uploadoffset, p.downloadoffset FROM '. TRACKER_PEERS_TABLE .' p LEFT JOIN '.ATTACHMENTS_TABLE." a ON p.torrent=a.attach_id LEFT JOIN ".TOPICS_TABLE." t ON a.post_msg_id=t.topic_first_post_id LEFT JOIN ".TRACKER_TORRENTS_TABLE." tt ON p.torrent=tt.id LEFT JOIN ".TRACKER_SNATCHED_TABLE." s ON s.torrent=p.torrent AND s.userid=p.userid WHERE p.userid='$user_id' AND p.seeder='no' ORDER BY $sql_addon";
$result=$db->sql_query($sql);
while($userlist=$db->sql_fetchrow($result))
{
	if(($userlist['userid']!=$user->data['user_id'] && !$is_admod) || !$user->data['is_registered'])
	{
		$userlist['ip']='';
		$userlist['port']='';
	}
	$i3+=1;
	$secs = max(10, ($userlist['last_action']) - $userlist['prev_action']);
	$assign_vars[$i3]['TORRENT_NAME'] = $userlist['topic_title'] ? "<a href='".append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$userlist['topic_first_post_id']}")."#p{$userlist['topic_first_post_id']}'>{$userlist['topic_title']}</a>" : '&nbsp;';
	$assign_vars[$i3]['TORRENT_UP'] = get_formatted_filesize($userlist['uploaded']);
	$assign_vars[$i3]['TORRENT_UPSP'] = get_formatted_filesize($userlist['uploadoffset'] / $secs, 1, false, 1);
	$assign_vars[$i3]['TORRENT_DOWN'] = get_formatted_filesize($userlist['downloaded']);
	$assign_vars[$i3]['TORRENT_DOWNSP'] = get_formatted_filesize($userlist['downloadoffset'] / $secs, 1, false, 1);
	$assign_vars[$i3]['TORRENT_RATIO'] = get_ratio($userlist['uploaded'], $userlist['downloaded']);
	$assign_vars[$i3]['TORRENT_COMPLETED'] = @sprintf("%.2f%%", 100 * (1 - ($userlist["to_go"] / $userlist['size'])));
	$assign_vars[$i3]['TORRENT_A'] = get_time_diff($userlist['startdat'], time());
	//$assign_vars[$i3]['TORRENT_INA'] = get_time_diff($userlist['last_action'], time());
	$assign_vars[$i3]['TORRENT_UCONN'] = $config['ppkbb_tcignore_connectable'] ? $user->lang['TORRENT_UNKNOWN_HEALTH'] : ($userlist['connectable']=='yes' ? $user->lang['YES'] : $user->lang['NO']);
	$assign_vars[$i3]['TORRENT_AGENT'] = htmlspecialchars($userlist['agent']);
	$assign_vars[$i3]['TORRENT_IP'] = htmlspecialchars($userlist['ip']);
	$assign_vars[$i3]['TORRENT_PORT'] = htmlspecialchars($userlist['port']);
}
$db->sql_freeresult($result);
$assigned_vars=sizeof($assign_vars);
$opt_sort=$opt_sort=='DESC' ? 'ASC' : 'DESC';
foreach($sort_opt[$torrent_opt] as $v)
{
	$template->assign_vars(array(
		'TORRENT_SORT_HEADER_'.strtoupper($v) => $assigned_vars > 1 ? "$ucp_url&amp;opt={$torrent_opt}&amp;opts={$opt_sort}&amp;optp={$v}#opt" : 'javascript:void(0);',
				)
			);
}
$torrent_info_curr=$torrent_opt;
$torrent_info[$torrent_opt]='<b>'.$torrent_info[$torrent_opt].'</b> ('.$i3.')';
$torrent_info_curr_explain[$torrent_opt]=$user->lang['USER_TORRENT_LEECH'];
?>
